package com.prime.tutorial.moviestore.view.admin;

import com.prime.tutorial.moviestore.domain.Groups;
import com.prime.tutorial.moviestore.domain.Users;
import com.prime.tutorial.moviestore.service.admin.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Date: 22 лют 2010
 * Time: 3:08:02
 * @author iryna.masyuk
 */
@Component("userCreate")
@Scope("request")
public class UserCreate implements Serializable {
    private AdminService adminService;

    //used to generate a list of groups for select menu
    private List availableGroups;

    private Users userToCreate;

    //used to store the result of select
    private String selectedGroup;

    @Autowired
    public UserCreate(AdminService adminService) {
        this.adminService = adminService;
        this.userToCreate = new Users();
        //list of group values for select list is created
        availableGroups = new ArrayList();
        List<Groups> groups = adminService.getAllGroups();
        for(int i=0; i<groups.size();i++){
            SelectItem option = new SelectItem(groups.get(i).getName(), groups.get(i).getName());
            availableGroups.add(option);
        }
    }
    //------------------------------------public methods
    public String createUser(ActionEvent actionEvent){
        //fields are not filled in
        if (userToCreate.getLogin().equals("") ||
            userToCreate.getPassword().equals("") ||
            userToCreate.getFirstName().equals("") ||
            userToCreate.getLastName().equals("")
                ) {
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Some of the required fields are not filled in", "");
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
            return null;     
        }

        //user with this login already exists
        if (adminService.loginExists(userToCreate.getLogin())){
            FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Users with this login is already in the database", "");
            FacesContext.getCurrentInstance().addMessage(null, facesMessage);
            return null;
        }

        //everything is OK - getting usergroup
        List<Groups> g = adminService.findByName(selectedGroup);
        userToCreate.setGroup(g.get(0));

        //setting dates
        userToCreate.setCreated(new Date());

        //TODO what is validity??
        userToCreate.setValidity(new Date());
        String text = userToCreate.getLogin();
        //adding user
        adminService.createNew(userToCreate);

        userToCreate = new Users();

 //       showMessage("Devices are added successfully");
         FacesContext context = FacesContext.getCurrentInstance();
        //TODO Find out how not to display the message twice.
         context.addMessage(null, new FacesMessage("User created successfully: " + text));
    //     context.addMessage(null, new FacesMessage("Second Message", "Additional Info Here..."));
  //      FacesMessage facesMessage = new FacesMessage(FacesMessage.SEVERITY_INFO, "Users is created successfully", "");
  //      FacesContext.getCurrentInstance().addMessage(null, facesMessage);
        return null;
    }


    //------------------------getters&setters
    public List getAvailableGroups() {
        return availableGroups;
    }

    public Users getUserToCreate() {
        return userToCreate;
    }

    public void setUserToCreate(Users userToCreate) {
        this.userToCreate = userToCreate;
    }

    public String getSelectedGroup() {
            return selectedGroup;
        }

    public void setSelectedGroup(String selectedGroup) {
            this.selectedGroup = selectedGroup;
        }
}